package yb.ecp.fast.oplog.service;


import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import yb.ecp.fast.feign.FastGenClient;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.oplog.entity.OpLog;
import yb.ecp.fast.oplog.mapper.OpLogMapper;
import yb.ecp.fast.user.infra.ErrorCode;

import java.util.Collections;

/**
 * OpLog 后台接口实现类
 */
@Slf4j
@Service
public class OpLogServiceImpl {

    @Autowired
    private OpLogMapper opLogMapper;

    @Autowired
    private FastGenClient fastGenClient;

    public void saveOpLog(OpLog opLog) {
        opLog.setId(textIdWithException());
        opLogMapper.batchInsert(Collections.singletonList(opLog));
    }


    public String textIdWithException() {
        ActionResult<String> textResult = fastGenClient.textGuid();
        if (textResult.getCode() != ErrorCode.Success.getCode() || StringUtils.isBlank(textResult.getValue())) {
            throw new RuntimeException("FastGenClient服务异常，请稍后重试");
        }
        return textResult.getValue();
    }

}
